package com.turingitclub.yutian.lesson03;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) {
        // 创建一个班级的学生，记录每个学生的基本信息：name age grade address sex，并且从中找出grade最优秀学生
        ArrayList<HashMap<String, Object>> students = new ArrayList<HashMap<String, Object>>();

        HashMap<String, Object> student = new HashMap<String, Object>();
        student.put("name", "张三");
        student.put("age", 20);
        student.put("grade", 80.5);
        student.put("address", "江苏南京");
        student.put("sex", true); // true-男
        students.add(student);

        student = new HashMap<String, Object>();
        student.put("name", "李四");
        student.put("age", 21);
        student.put("grade", 90.5);
        student.put("address", "江苏苏州");
        student.put("sex", true); // true-男
        students.add(student);

        student = new HashMap<String, Object>();
        student.put("name", "小红");
        student.put("age", 19);
        student.put("grade", 100);
        student.put("address", "江苏常州");
        student.put("sex", false); // true-男
        students.add(student);

        student = new HashMap<String, Object>();
        student.put("name", "王五");
        student.put("age", 29);
        student.put("grade", 160);
        student.put("address", "江苏无锡");
        student.put("sex", true); // true-男
        students.add(student);

        System.out.println(students);

        int size = students.size();
        HashMap<String, Object> maxGrade = null;
        for(int i=0; i<size; i++){
            HashMap<String, Object> stu = students.get(i);
            System.out.println(stu);
            if(null == maxGrade){
                maxGrade = stu;
                continue;
            }
            double grade = Double.valueOf(stu.get("grade").toString());
            double gradeMax = Double.valueOf(maxGrade.get("grade").toString());
            if(grade > gradeMax){
                maxGrade = stu;
            }
        }
        System.out.println(maxGrade);
//        String name = (String)student.get("name");
//        Integer age = (Integer)student.get("age");
//        System.out.println(student);

    }













    private static void plusPlus() {
        int i = 1;
        System.out.println(i++); // 输出1，得到1+1=2
        System.out.println(++i); // 得到2+1=3，输出3
        System.out.println(i);

        System.out.println(i--); // 输出3，得到3-1=2
        System.out.println(--i); // 得到2-1=1，输出1
    }

    private static void getLinkedList(LinkedList<Integer> list2) {
        list2.add(333);
        // 双向链表
        LinkedList<Integer> list = null;
        list = new LinkedList<Integer>();
        list.add(12);
        list.add(13);
        System.out.println(list.size());
        list.set(0,22);
        list.remove(0);
        System.out.println(list.get(0));
    }

    private static void getArrayList() {
        // ArrayList 本质是数组Object[] elementData
        ArrayList<Integer> list = null;
        list = new ArrayList<Integer>();

        list.add(12);
        list.add(13);
        //list.add("addf");
        //list.set(0, 1);
        // 下标从0开始，集合变量.get(下标)得到某个元素。下标，又称作索引！
        System.out.println(list.get(0)); // IndexOutOfBoundsException
        list.remove(0);
        System.out.println(list.size());
        System.out.println(list.get(0));
        list.remove(Integer.valueOf(0));
        System.out.println(list.size());
        System.out.println(list.size());
    }

    private static void getArr() {
        Integer[] arr = null;
        System.out.println(arr);
        arr = new Integer[]{11,12};
        // 下标从0开始，数组变量[下标]得到某个元素。下标，又称作索引！
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr.length);

        // 默认的元素是null
        arr = new Integer[2];
        arr[0] = 1;
        arr[1] = 2;
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr.length);

        Boolean[] arrb = null;

        String[] arrS = null;

        int[] arr2 = null;

        arr2 = new int[]{111};
        System.out.println(arr2[0]);
    }

    private static void getDate() {
        Date now = new Date();
        System.out.println(now);

        now = Calendar.getInstance().getTime();

        // Calendar.getInstance().getFirstDayOfWeek();

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String current = sf.format(now);
        System.out.println(current);

        sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        current = sf.format(now);
        System.out.println(current);

        // 禁止使用YYYY ： 当日所在周的最后一天所属的年份
        sf = new SimpleDateFormat("YYYY-MM-dd");
        current = sf.format(now);
        System.out.println(current);

        // 2010-12-26
        try {
            sf = new SimpleDateFormat("yyyy-MM-dd");
            Date t = sf.parse("2010-12-26");
            sf = new SimpleDateFormat("YYYY-MM-dd");
            current = sf.format(now);
            System.out.println(current);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private static void getString() {
        String str = "abc";
        System.out.println(str);
        System.out.println(str.toString());

        str = "bcd";
        System.out.println(str);

        System.out.println(str.equals("bcd"));

        str = String.valueOf(124);
        System.out.println(str);
        str = ((Integer)300).toString();
        System.out.println(str);
        str = 600 + "";
        System.out.println(str);

        str = 600 + "123"; // "600" + "123", 拼接=600123 =>
        // 本质是：1）"600" + "123" ；2）StringBuilder sb = new StringBuilder(); 3）sb.append("600").append("123") 4） str = sb.toString()
        System.out.println(str);

        // 本质是：1）"600" + "123" + "345"；2）StringBuilder sb = new StringBuilder(); 3）sb.append("600").append("123") 4） temp = sb.toString()
        // 5）StringBuilder sb2 = new StringBuilder(); 6）sb2.append(temp).append("345") 4） temp = sb2.toString()
        str = 600 + "123" + 345;

        StringBuilder sb = new StringBuilder();
        sb.append(600).append("123").append(345);
        System.out.println(sb.toString());
    }

    /**
     * 类型转换
     * 1.Integer.valueOf实现类型转换
     * 2.包装类型可以转基本类型
     * 注意：短类型转长类型--right;长类型转短类型--error
     * @param by
     */
    private static void byte2int(Byte by) {
        Integer len = 300;

        len = Integer.valueOf(by);
        System.out.println( len );

        //Integer lenb = (Integer) by;
        int ilen = by; // 解包：包装类型可以转基本类型 ； 短类型转长类型--right
        System.out.println( ilen );

        byte b = (byte)300; // 转换失败: 长类型转短类型--error
        System.out.println( b );
    }

    private static Byte getaByte() {
        // 2^(8-1)-1 预估：+-100以内
        Byte by = 100;
        System.out.println( by );

//        by = 300;
//        System.out.println( by );
        return by;
    }

    /**
     * 静态方法
     * @param bbbb
     */
    private static void getBoolean(Boolean bbbb) {
        // 基本类型，声明时必须赋值，且不为null
        // boolean bb = null;

        // 包装类型，可赋值为null
        Boolean bb = null; // null 是一个对象，即实例
        System.out.println( bb);

        // 包装类型，声明时不必须赋值，使用时必须先赋值
        Boolean bbb;
        //System.out.println( bbb);

        System.out.println( bbbb);

        boolean ok = true;
        System.out.println( ok);

        // 加包
        Boolean okAlia = ok;

        System.out.println( okAlia);
        System.out.println( okAlia.toString());

        // 解包
        ok = okAlia;

        ok = false;
        System.out.println( okAlia);
        System.out.println( ok);
    }

    private static Integer getInteger() {
        int age = 21;
        System.out.println( age);

        // int 范围：4字节，每个字节8位，共32位，每一位0/1，2^32，首位是表示正和负，
        // 01111111 11111111 11111111 11111111，简单记忆：不要大于10亿
        System.out.println( Integer.MAX_VALUE);
        // 负数用补码表示=正数反码+1，简单记忆：不要小于10亿
        System.out.println( Integer.MIN_VALUE);

        // 加包
        Integer ageAlia = age;

        System.out.println( ageAlia);
        // 等价于
        System.out.println( String.valueOf(ageAlia));

        System.out.println( ageAlia.toString());

        // 解包
        age = ageAlia;


        return ageAlia;
    }
}
